package com.meepo.sdk.component.view.fragment

import android.util.Log
import androidx.annotation.CallSuper

/**
 */
abstract class LoadOnceOnVisibleFragment : BaseSupportFragment() {

    // 方便子类直接使用 判断当前fragment是否在展示
    protected var mVisible = false

    private var loaded = false


    // 可以删掉  已经包含在 onSupportVisible 中
    @CallSuper
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        Log.d(this.javaClass.name, "onHiddenChanged $hidden")

        mVisible = !hidden
        doLoadDataOnce()
    }

    @CallSuper
    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        Log.d(this.javaClass.name, "setUserVisibleHint $isVisibleToUser")

        mVisible = isVisibleToUser
        doLoadDataOnce()
    }

    @CallSuper
    override fun onSupportVisible() {
        super.onSupportVisible()
        Log.d(this.javaClass.name, "onSupportVisible")

        mVisible = true
        doLoadDataOnce()
    }

    @Synchronized
    private fun doLoadDataOnce() {

        if (refreshOnVisible()) {
            initData()
        } else {
            if (mVisible && !loaded) {
                initData()
                loaded = true
            }
        }
    }


    open fun refreshOnVisible(): Boolean = false

}